package com.bdd.jpa.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bdd.jpa.util.entity.ParamsObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Bdd
 * @Description
 *      参数转换
 * @Date 2019/9/24 17:28
 */
public class ParamsUtil {


    /**
     * 接受List对象
     * @param list
     * @return
     * @throws JsonProcessingException
     */
    public static String paramsListToJSON(List<ParamsObject> list) throws JsonProcessingException {
        String jsonStr = JSON.toJSONString(list);
        return jsonStr;
    }

    /**
     * 接受数组对象
     * 把需要参数类型转成JSON字符串
     * @return
     */
    public static String paramsToJSON(Object...params) throws JsonProcessingException {
        String jsonStr = JSON.toJSONString(params);
        return jsonStr;
    }

    /**
     * 把json字符串转成List<ParamsObject>
     * @param json
     * @return
     * @throws IOException
     */
    public static List<ParamsObject> JSONtoParamsObject(String json) throws IOException {
        List<ParamsObject> list = JSON.parseArray(json,ParamsObject.class);
        return list;
    }

    public static void main(String[] args) throws IOException {
        ParamsObject paramsObject = new ParamsObject("java.lang.String","name");
        String json = ParamsUtil.paramsToJSON(paramsObject);
        System.out.println(json);
        List list = ParamsUtil.JSONtoParamsObject(json);
        list.forEach(item-> System.out.println(item.getClass().getName()));
    }

}
